package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.TypePop
import hlhj.fhp.supreme.javabean.ClassListBean
import hlhj.fhp.supreme.javabean.PhoneBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_class_aty.*
import java.util.ArrayList

class ClassAty : BaseAty(), SpringView.OnFreshListener {
    private var des=""
    private lateinit var adp:BaseMultiItemQuickAdapter<ClassListBean.DataBeanX,BaseViewHolder>
    private val datas=ArrayList<ClassListBean.DataBeanX>()
    override fun getContentId(): Int {
        return R.layout.activity_class_aty
    }

    override fun initView() {

        adp =object :BaseMultiItemQuickAdapter<ClassListBean.DataBeanX,BaseViewHolder>(datas){
            init {
                addItemType(-1,R.layout.read_head)
                addItemType(1,R.layout.class_list_item)
                addItemType(0,R.layout.class_list_item)
            }
            override fun convert(helper: BaseViewHolder?, item: ClassListBean.DataBeanX?) {
                when(helper?.itemViewType){
                    -1->{
                        helper?.setText(R.id.tv2,des)
                        helper?.setVisible(R.id.btVip,false)

                    }
                    1->{
                        var lid="1"
                        helper?.getView<TextView>(R.id.btMore).setOnClickListener {
                            if (item?.data==null||item?.data.size==0){
                                ToastUtils.toast(mContext,"没有更多了")
                                return@setOnClickListener
                            }else{
                                val intent = Intent(this@ClassAty, ClassFilterAty::class.java)
                                intent.putExtra("cat_id",item?.id.toString())
                                intent.putExtra("tittle",item?.name)
                                intent.putExtra("lid",lid)
                                startActivity(intent)
                            }
                        }
                        helper?.setText(R.id.tv1,item?.name)
                        val tvYear = helper?.getView<TextView>(R.id.tvYear)
                        tvYear.text="请选择"
//                        筛选点击
                        val types=ArrayList<ClassListBean.DataBeanX.LevelBean>()
                        types.addAll(item?.level!!)
                        val typePop = TypePop(mContext, types, object : TypePop.OnTypeSelectListener {
                            override fun onTypeSelect(id: String,str:String) {
                                lid=id
                                helper?.setText(R.id.tvYear,str)
                                OkGo.post<ClassListBean>(Urls.BaseUrl + Urls.CLASS_LIST)
                                        .params("cat_id", item?.id)
                                        .params("level_id", id)
                                        .execute(object : JsonCallBack<ClassListBean>(this@ClassAty) {
                                            override fun onSuccess(response: Response<ClassListBean>?) {
                                                LogUtil.logi("点击事件")
                                                val bean = response?.body()
                                                if (bean?.code==1){
                                                    val datas=ArrayList<ClassListBean.DataBeanX.DataBean>()
                                                    val listView = helper?.getView<RecyclerView>(R.id.recyclerview)
                                                    LogUtil.logi("点击事件大小${bean?.data.size}")
                                                    if (null!=bean?.data[0].data){
                                                        if (bean?.data[0].data.size>2){
                                                            datas.add(bean?.data[0].data[0])
                                                            datas.add(bean?.data[0].data[1])
                                                        }else{
                                                            datas.addAll(bean?.data[0].data)
                                                        }
                                                    }

                                                    val adp  =  object :BaseQuickAdapter<ClassListBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.class_item,datas){
                                                        override fun convert(helper: BaseViewHolder?, itt: ClassListBean.DataBeanX.DataBean?) {
                                                            helper?.setText(R.id.itemName,itt?.title)
                                                            helper?.setText(R.id.itemPrice,"￥"+itt?.money)
                                                            helper?.setText(R.id.itemTime,MyUtils.timeStampToStrYMD(itt?.create_time!!.toLong()))
                                                            Glide.with(mContext).load(itt?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                                            val lo = helper?.getView<RelativeLayout>(R.id.lo)
                                                            lo?.setOnClickListener {
                                                                val intent = Intent(this@ClassAty, ClassDetailAty::class.java)
                                                                intent.putExtra("id",itt?.id)
                                                                intent.putExtra("lid",itt?.level_id.toString())
                                                                startActivity(intent)
                                                            }
                                                        }
                                                    }
                                                    listView?.adapter=adp
                                                    listView?.layoutManager=GridLayoutManager(mContext,2)

                                                }else{
                                                    MyUtils.toast(bean?.msg)
                                                }
                                            }
                                        })
                            }
                        })

                        tvYear.setOnClickListener {

                            if (typePop.isShowing){
                                typePop.dismiss()
                            }else{
                                typePop.showAsDropDown(tvYear)
                            }

                        }

                        if (item?.data!=null){
                            val listView = helper?.getView<RecyclerView>(R.id.recyclerview)
                            val datas=ArrayList<ClassListBean.DataBeanX.DataBean>()
                            if (item?.data.size>2){
                                datas.add(item?.data[0])
                                datas.add(item?.data[1])
                            }else{
                                datas.addAll(item?.data)
                            }
                            val adp  =  object :BaseQuickAdapter<ClassListBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.class_item,datas){
                                override fun convert(helper: BaseViewHolder?, itt: ClassListBean.DataBeanX.DataBean?) {
                                    helper?.setText(R.id.itemName,itt?.title)
                                    helper?.setText(R.id.itemPrice,"￥"+itt?.money)
                                    helper?.setText(R.id.itemTime,MyUtils.timeStampToStrYMD(itt?.create_time!!.toLong()))
                                    Glide.with(mContext).load(itt?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    val lo = helper?.getView<RelativeLayout>(R.id.lo)
                                    lo?.setOnClickListener {
                                        val intent = Intent(this@ClassAty, ClassDetailAty::class.java)
                                        intent.putExtra("id",itt?.id)
                                        LogUtil.e("fhpp",itt?.level_id.toString())
                                        intent.putExtra("lid",itt?.level_id.toString())
                                        startActivity(intent)
                                    }
                                }
                            }
                            listView?.adapter=adp
                            listView?.layoutManager=GridLayoutManager(mContext,2)
                        }
                    }
                }

            }
        }

        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        getData()
    }

    private fun getData() {
        OkGo.post<ClassListBean>(Urls.BaseUrl+Urls.CLASS_LIST)
                .execute(object :JsonCallBack<ClassListBean>(this){
                    override fun onSuccess(response: Response<ClassListBean>?) {
                        spView.onFinishFreshAndLoad()
                        val bean = response?.body()
                        if (bean?.code==1){
                            val list = bean?.data
                            for (i in 0..list.size-1){
                                list[i].setType(1)
                            }
                            val head = ClassListBean.DataBeanX()
                            head.setType(-1)
                            list.add(0,head)
                            datas.addAll(list)
                            adp.notifyDataSetChanged()
                        }
                    }
                })
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","class")
                .execute(object :JsonCallBack<PhoneBean>(this){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val body = response!!.body()
                        if (body?.code==1){
                            des=body.data.classX
                        }else{
                            ToastUtils.toast(this@ClassAty,body?.msg!!)
                        }
                    }
                })
    }

    override fun initListener() {
        spView.header=DefaultHeader(this)
        spView.setListener(this)
        btExit.setOnClickListener {
            finish()
        }
    }

    override fun onLoadmore() {
    }

    override fun onRefresh() {
        datas.clear()
        getData()
    }
}
